package com.wuyan.locationdog.ui.my

import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.DataBean
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_fence_refresh
import com.arcsoft.commonbase.bus.bus_toast
import com.arcsoft.commonbase.ext.afterTextChange
import com.arcsoft.commonbase.ext.btnEnable
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.textStringTrim
import com.arcsoft.commonbase.utils.getTestDataList
import com.arcsoft.commonbase.widgets.dialog.ToastBean
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.MarkSetListAdapter
import com.wuyan.locationdog.adapter.TeamListAdapter
import com.wuyan.locationdog.adapter.TestListAdapter
import com.wuyan.locationdog.databinding.MarkSetActivityBinding
import com.wuyan.locationdog.databinding.TeamListActivityBinding
import com.wuyan.locationdog.help.DogHelp
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class MarkSetNameActivity : BaseBindingActivity<MarkSetActivityBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { MarkSetListAdapter(R.layout.item_mark_name, null) }
    val list = mutableListOf<DataBean>()

    override fun initData() {

        setTitleText("标记点设置")

        binding.apply {
            btnLast.setOnClickListener {
                finish()
            }
            btnSure.setOnClickListener {
                val map = HashMap<String, Any>()
                map["pointName"] = pointName.textStringTrim()
//                map["pointId"] = DogHelp.pointPoiItem?.poiId.toString()
                map["pointLat"] = DogHelp.pointPoiItem?.latLonPoint?.latitude.toString()
                map["pointLng"] = DogHelp.pointPoiItem?.latLonPoint?.longitude.toString()
                map["pointType"] = list.indexOfFirst { it.isSelect } + 1

                mViewModel.pointAdd(map)
            }
            btnEnable(btnSure, list.find { it.isSelect }?.name ?: "", pointName)
            pointName.afterTextChange {
                btnEnable(btnSure, list.find { it.isSelect }?.name ?: "", pointName)
            }
            recyclerView.apply {
                layoutManager = GridLayoutManager(context, 5)
                adapter = mAdapter.apply {
                    list.addAll(DogHelp.getMarkList())

                    setNewInstance(list)
                    setOnItemClickListener { adapter, view, position ->
                        data.find { it.isSelect }?.isSelect = false
                        data[position].isSelect = true
                        notifyDataSetChanged()
                        pointName.setText(data[position].name)
                        btnEnable(btnSure, list.find { it.isSelect }?.name ?: "", pointName)

                    }

                }
            }


        }
    }

    override fun loadNetData() {
        super.loadNetData()

    }

    override fun observe() {
        super.observe()
        Bus.observe<String>(bus_fence_refresh, this) {
            loadNetData()
        }
        mViewModel.fenceBean.observe(this) {


        }
        mViewModel.anyBean.observe(this) {

            ActivityUtils.finishActivity(MarkMapActivity::class.java)
            finish()

            Bus.post(bus_toast, ToastBean("保存成功"))

        }
    }
}